package com.kongjs.online.preview.utils;

import com.documents4j.api.DocumentType;
import com.documents4j.conversion.msoffice.MicrosoftPowerpointBridge;
import com.documents4j.conversion.msoffice.MicrosoftWordBridge;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.concurrent.TimeUnit;

public class MsOfficeUtils {
    public static void wordToPdf(File baseFolder, long processTimeout, TimeUnit processTimeoutUnit, File source, File target) {
        String filenameExtension = StringUtils.getFilenameExtension(source.getName());
        if (filenameExtension == null) {
            throw new NullPointerException();
        }
        DocumentType sourceType = null;
        if (filenameExtension.equalsIgnoreCase("docx")) {
            sourceType = DocumentType.DOCX;
        } else if (filenameExtension.equalsIgnoreCase("doc")) {
            sourceType = DocumentType.DOC;
        }
        if (sourceType == null) {
            throw new IllegalStateException();
        }
        MicrosoftWordBridge wordBridge = new MicrosoftWordBridge(baseFolder, processTimeout, processTimeoutUnit);
        wordBridge.startConversion(source, sourceType, target, DocumentType.PDF);
        wordBridge.shutDown();
    }

    public static void pptToPdf(File baseFolder, long processTimeout, TimeUnit processTimeoutUnit, File source, File target) {
        String filenameExtension = StringUtils.getFilenameExtension(source.getName());
        if (filenameExtension == null) {
            throw new NullPointerException();
        }
        DocumentType sourceType = null;
        if (filenameExtension.equalsIgnoreCase("pptx")) {
            sourceType = DocumentType.PPTX;
        } else if (filenameExtension.equalsIgnoreCase("ppt")) {
            sourceType = DocumentType.PPT;
        }
        if (sourceType == null) {
            throw new IllegalStateException();
        }
        MicrosoftPowerpointBridge powerpointBridge = new MicrosoftPowerpointBridge(baseFolder, processTimeout, processTimeoutUnit);
        powerpointBridge.startConversion(source, sourceType, target, DocumentType.PDF);
        powerpointBridge.shutDown();
    }
    public static void main(String[] args) {
    }
}

